package com.tanhua.dubbo.api.impl.mongo;

import com.tanhua.domain.mongo.TaoHuaSound;
import com.tanhua.dubbo.api.mongo.TaoHuaSoundApi;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

@Service
public class TaoHuaSoundApiImpl implements TaoHuaSoundApi {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void save(TaoHuaSound taoHuaSound) {
        mongoTemplate.save(taoHuaSound);
    }

    @Override
    public TaoHuaSound getVoice(String id) {
        Query query = new Query(Criteria.where("_id").is(new ObjectId(id)).and("state").is(0));
        TaoHuaSound taoHuaSound = mongoTemplate.findOne(query, TaoHuaSound.class);
        Update update = new Update();
        update.set("state",1);
        mongoTemplate.updateFirst(query,update,TaoHuaSound.class);
        return taoHuaSound;
    }
}
